<template>
  <div>
    <a-card title="星弹直传启动/终止设置(车→地)">
      <a slot="extra" href="#" @click="showModal">详情</a>
      <a-table class="j-table-force-nowrap" :scroll='{x:true}' bordered :columns="columns" size="small" :data-source="data" :pagination="false"></a-table>
    </a-card>
    <a-modal v-model="visible" title="历史信息" @ok="handleOk" width="60%">
      <a-table :columns="columns" :data-source="dataHistory"></a-table>
    </a-modal>
  </div>
</template>

<script>
import { getAction } from '@/api/manage'
import { mapGetters } from 'vuex'
export default {
  name: "DirectTransmissionStartStop",
  data() {
    const columns = [
      {
        title: '指控车',
        dataIndex: 'commandVehicleName',
        key: 'commandVehicleName',
        customRender: (value, row, index) => {
          const obj = {
            children: value,
            attrs: {},
          }
          obj.attrs.rowSpan = row.commandVehicleNamerowSpan
          return obj
        },
      },

      {
        title: '导弹识别码',
        dataIndex: 'missileIdentificationCode',
        key: 'missileIdentificationCode',
      },

      {
        title: '直传启动/停止状态',
        dataIndex: 'directTransmissionState_dictText',
        key: 'directTransmissionState_dictText',
        // customRender: function (t) {
        //   if (t == '00') {
        //     return '停止'
        //   } else if (t == '01') {
        //     return '启动'
        //   } else {
        //     return ''
        //   }
        // }
      },
      {
        title: '校验结果',
        dataIndex: 'verifyResult_dictText',
        key: 'verifyResult_dictText',

      },


    ];
    const data = [
      // {
      //   key: '0',
      //   commandVehicleName: '指控车1',
      //   identificationCode: '0100',
      //   verificationResults: '00',
      //   targetNum: '124',
      //   StartOrStop: '00'
      // },
      // {
      //   key: '0-1',
      //   commandVehicleName: '指控车1',
      //   identificationCode: '0101',
      //   verificationResults: '00',
      //   targetNum: '124',
      //   StartOrStop: '00'
      // }, {
      //   key: '1',
      //   commandVehicleName: '指控车2',
      //   identificationCode: '0200',
      //   verificationResults: '01',
      //   targetNum: '124',
      //   StartOrStop: '01',
      // },
      // {
      //   key: '2',
      //   commandVehicleName: '指控车3',
      //   identificationCode: '0301',
      //   verificationResults: '01',
      //   targetNum: '124',
      //   StartOrStop: '00'
      // },
      // {
      //   key: '3',
      //   commandVehicleName: '指控车4',
      //   identificationCode: '0401',
      //   verificationResults: '00',
      //   targetNum: '124',
      //   StartOrStop: '00'
      // },
    ];
    return {
      data,
      columns,
      visible: false,
      dataHistory: [],
      url: {
        list: '/smDirectTransmission/nmcSmMainDirectTransmission/queryRealTimeList',
        historyList: '/KeyTrackingAreas/historyList'
      }
    };
  },
  computed: {
    ...mapGetters(['websocketMsg']),
    websocketMsg() {
      return this.$store.state.user.websocketMsg
    },
  },
  watch: {
    websocketMsg: {
      handler(val) {
        // 需要进行的操作
        if(val){
          let msg = JSON.parse(val)
          if (msg.cmd === 'RECEIVE_CONTROL_SATELLITE') {
            this.getList();
          }
        }
      },
    },
  },
  activated() {
    this.getList()
  },
  created() {
    this.getList()
  },
  methods: {
    changeData(data, field) {
      let count = 0 //重复项的第一项
      let indexCount = 1 //下一项
      while (indexCount < data.length) {
        var item = data.slice(count, count + 1)[0] //获取没有比较的第一个对象
        if (!item[`${field}rowSpan`]) {
          item[`${field}rowSpan`] = 1 //初始化为1
        }
        if (item[field] === data[indexCount][field]) {
          //第一个对象与后面的对象相比，有相同项就累加，并且后面相同项设置为0
          item[`${field}rowSpan`]++
          data[indexCount][`${field}rowSpan`] = 0
        } else {
          count = indexCount
        }
        indexCount++
      }
      return data
    },
    getList() {
      getAction(this.url.list).then(res => {
        if (res.success) {
          this.data = res.result
          let propsList = ['commandVehicleName']
          propsList.map((item) => {
            this.data = [...this.changeData(this.data, item)]
          })
          // this.data = []
          // res.result.forEach(item => {
          //   let tempData = {}
          //   if (item.subDirectTransmissionVoList) {
          //     item.subDirectTransmissionVoList.forEach((ik, index) => {
          //       tempData = {
          //         "commandVehicleNamerowSpan": index == 0 ? item.subDirectTransmissionVoList.length : 0,
          //         "commandVehicleId": item.commandVehicleId,
          //         "commandVehicleName": item.commandVehicleName,
          //         ...ik
          //       }
          //       this.data.push(tempData)
          //     })
          //   } else {
          //     tempData = {
          //       "commandVehicleNamerowSpan": 0,
          //       "commandVehicleId": item.commandVehicleId,
          //       "commandVehicleName": item.commandVehicleName,
          //       "createBy": "",
          //       "createTime": "",
          //       "directTransmissionState": "",
          //       "directTransmissionState_dictText": "",
          //       "id": "",
          //       "missileIdentificationCode": "",
          //       "parentId": "",
          //       "sysOrgCode": "",
          //       "updateBy": "",
          //       "updateTime": ""
          //     }
          //     this.data.push(tempData)
          //   }
          // })

        } else {
          this.$message.error(res.message)
        }

      })
      // let propsList = ['commandVehicleName']
      // propsList.map((item) => {
      //   this.data = [...this.changeData(this.data, item)]
      // })
    },

    showModal() {
      this.visible = true;
      getAction(this.url.historyList).then(res => {
        if (res.success) {
          this.dataHistory = res.result.records
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleOk(e) {
      this.visible = false;
    },
  }
}
</script>

<style>
</style>